package cn.powerone.scm.service.impl;

import java.util.Date;
import java.util.List;

import cn.powerone.common.exception.ServiceException;
import cn.powerone.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import cn.powerone.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import cn.powerone.scm.domain.ScmSupplierContacts;
import cn.powerone.scm.mapper.ScmSupplierMapper;
import cn.powerone.scm.domain.ScmSupplier;
import cn.powerone.scm.service.IScmSupplierService;

/**
 * 供应商Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-06-23
 */
@Service
public class ScmSupplierServiceImpl implements IScmSupplierService 
{
    @Autowired
    private ScmSupplierMapper scmSupplierMapper;

    /**
     * 查询供应商
     * 
     * @param id 供应商主键
     * @return 供应商
     */
    @Override
    public ScmSupplier selectScmSupplierById(Long id)
    {
        return scmSupplierMapper.selectScmSupplierById(id);
    }

    /**
     * 查询供应商列表
     * 
     * @param scmSupplier 供应商
     * @return 供应商
     */
    @Override
    public List<ScmSupplier> selectScmSupplierList(ScmSupplier scmSupplier)
    {
        return scmSupplierMapper.selectScmSupplierList(scmSupplier);
    }

    /**
     * 新增供应商
     * 
     * @param scmSupplier 供应商
     * @return 结果
     */
    @Transactional
    @Override
    public int insertScmSupplier(ScmSupplier scmSupplier)
    {
        scmSupplier.setCreateTime(DateUtils.getNowDate());
        int rows = scmSupplierMapper.insertScmSupplier(scmSupplier);
        insertScmSupplierContacts(scmSupplier);
        return rows;
    }

    /**
     * 修改供应商
     * 
     * @param scmSupplier 供应商
     * @return 结果
     */
    @Transactional
    @Override
    public int updateScmSupplier(ScmSupplier scmSupplier)
    {
        scmSupplier.setUpdateTime(DateUtils.getNowDate());
        scmSupplierMapper.deleteScmSupplierContactsBySupplierId(scmSupplier.getId());
        insertScmSupplierContacts(scmSupplier);
        return scmSupplierMapper.updateScmSupplier(scmSupplier);
    }

    /**
     * 批量删除供应商
     * 
     * @param ids 需要删除的供应商主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScmSupplierByIds(Long[] ids)
    {
        scmSupplierMapper.deleteScmSupplierContactsBySupplierIds(ids);
        return scmSupplierMapper.deleteScmSupplierByIds(ids);
    }

    /**
     * 删除供应商信息
     * 
     * @param id 供应商主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScmSupplierById(Long id)
    {
        scmSupplierMapper.deleteScmSupplierContactsBySupplierId(id);
        return scmSupplierMapper.deleteScmSupplierById(id);
    }

    /**
     * 新增供应商联系人信息
     * 
     * @param scmSupplier 供应商对象
     */
    public void insertScmSupplierContacts(ScmSupplier scmSupplier)
    {
        List<ScmSupplierContacts> scmSupplierContactsList = scmSupplier.getScmSupplierContactsList();
        Long id = scmSupplier.getId();
        if (StringUtils.isNotNull(scmSupplierContactsList))
        {
            List<ScmSupplierContacts> list = new ArrayList<ScmSupplierContacts>();
            for (ScmSupplierContacts scmSupplierContacts : scmSupplierContactsList)
            {
                scmSupplierContacts.setSupplierId(id);
                list.add(scmSupplierContacts);
            }
            if (list.size() > 0)
            {
                scmSupplierMapper.batchScmSupplierContacts(list);
            }
        }
    }

    /** 导入数据
     * @param scmSupplier 导入询价Batch列表
     */
    @Transactional
    @Override
    public String importData(List<ScmSupplier> scmSupplier, boolean updateSupport, String operName){
        if (StringUtils.isNull(scmSupplier) || scmSupplier.size() == 0)
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        ScmSupplier customerInq = new ScmSupplier();
        long numberOfInq = 0;
        for (ScmSupplier item : scmSupplier)
        {
            if(item.getSupplierNo() != null && !item.getSupplierNo().isEmpty() && !updateSupport){
                customerInq.setSupplierNo(item.getSupplierNo());
                numberOfInq = scmSupplierMapper.selectScmSupplierList(customerInq).size();
                if(numberOfInq > 0){
                    continue;
                }
            }
            try
            {
                scmSupplierMapper.insertScmSupplier(item);
                successNum++;
            } catch (Exception e)
            {
                failureNum++;
            }
        }
        if (failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 供应商条数据格式不正确");
            throw new ServiceException(failureMsg.toString());
        }else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }

    @Override
    public int selectSupplierCountByMonth(Date yesterday) {
        return scmSupplierMapper.selectSupplierCountByMonth(yesterday);
    }

    @Override
    public int selectSupplierCountByYear(Date yesterday) {
        return scmSupplierMapper.selectSupplierCountByYear(yesterday);
    }

    @Override
    public int selectSupplierCountAll() {
        return scmSupplierMapper.selectSupplierCountAll();
    }
}
